﻿#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
using namespace std;


class Car {
public:
	virtual void Dirve()
	{}
};
class Benz :public Car {
public:
	// error C3668: “Benz::Drive”: 包含重写说明符“override”的方法没有重写任何基类⽅法
	//virtual void Drive() override { cout << "Benz-舒适" << endl; }
	//virtual void Dirve() override { cout << "Benz-舒适" << endl; }// BinGou

};

// class Car final 一个无法被继承的类
class Car
{
public:
	virtual void Drive() final {}
};
class Benz :public Car
{
public:
	// error C3248: “Car::Drive”: 声明为“final”的函数无法被“Benz::Drive”重写
	virtual void Drive() { cout << "Benz-舒适" << endl; }
};

class Car //抽象类无法实例化出对象
{
public:
	virtual void Drive() = 0;// 虚函数
};
class Benz :public Car
{
public:
	virtual void Drive()
	{
		cout << "Benz-舒适" << endl;
	}
};
class BMW :public Car
{
public:
	virtual void Drive()
	{
		cout << "BMW-操控" << endl;
	}
};
int main()
{
	// 编译报错：error C2259: “Car”: ⽆法实例化抽象类
	//Car car;
	Car* pBenz = new Benz;
	pBenz->Drive();
	Car* pBMW = new BMW;
	pBMW->Drive();
	return 0;
}

class Base
{
public:
	virtual void Func1()
	{
		cout << "Func1()" << endl;
	}
	int _b = 1;
	char _ch = 'x';
};
int main()
{
	Base b;
	cout << sizeof(b) << endl;
	cout << sizeof(b._b) << endl;
	cout << sizeof(b._ch) << endl;
	return 0;
}


class Person {
public:
	virtual void BuyTicket() { cout << "买票-全价" << endl; }
	int _name;
};
class Student : public Person {
public:
	virtual void BuyTicket() { cout << "买票-打折" << endl; }
	int _id;
};
class Soldier : public Person {
public:
	virtual void BuyTicket() { cout << "买票-优先" << endl; }
	int _age;
};
void Func(Person* ptr)
{
	// 这里可以看到虽然都是Person指针Ptr在调⽤BuyTicket
	// 但是跟ptr没关系，⽽是由ptr指向的对象决定的。
	ptr->BuyTicket();
}
int main()
{
	// 其次多态不仅仅发生在派生类对象之间，多个派生类继承基类，重写虚函数后
	// 多态也会发⽣在多个派生类之间。
	Person ps;
	Student st;
	Soldier sr;
	Func(&ps);
	Func(&st);
	Func(&sr);
	return 0;
}

class Base {
public:
	virtual void func1() { cout << "Base::func1" << endl; }
	virtual void func2() { cout << "Base::func2" << endl; }
	void func5() { cout << "Base::func5" << endl; }
protected:
	int a = 1;
};
class Derive : public Base
{
public:
	// 重写基类的func1
	virtual void func1() { cout << "Derive::func1" << endl; }
	virtual void func3() { cout << "Derive::func1" << endl; }
	void func4() { cout << "Derive::func4" << endl; }
protected:
	int b = 2;
};

//int main()
//{
//	Base b;
//	Derive d;
//	return 0;
//}

int main()
{
	int i = 0;
	static int j = 1;
	int* p1 = new int;
	const char* p2 = "xxxxxxxx";
	printf("栈:%p\n", &i);
	printf("静态区:%p\n", &j);
	printf("堆:%p\n", p1);
	printf("常量区:%p\n", p2);
	Base b;
	Derive d;
	Base* p3 = &b;
	Derive* p4 = &d;
	printf("Person虚表地址:%p\n", *(int*)p3);
	printf("Student虚表地址:%p\n", *(int*)p4);
	printf("虚函数地址:%p\n", &Base::func1);
	printf("普通函数地址:%p\n", &Base::func5);
	return 0;
}